package com.wlts.wzy.SelfModule.controller;


import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.wlts.wzy.Base.BaseEntity;
import com.wlts.wzy.Base.BaseIdParam;
import com.wlts.wzy.Result.Result;
import com.wlts.wzy.SelfModule.entity.SelfGoods;
import com.wlts.wzy.SelfModule.entity.SelfMyGoods;
import com.wlts.wzy.SelfModule.service.ISelfGoodsService;
import com.wlts.wzy.SelfModule.service.ISelfMyGoodsService;
import com.wlts.wzy.api.request.lm.LmToMyGoodsRequest;
import com.wlts.wzy.api.request.self.PageSelfMyGoodsRequest;
import com.wlts.wzy.expand.MyLambdaQueryWrapper;
import com.wlts.wzy.expand.MyPage;
import com.wlts.wzy.util.CopyUtils;
import com.wlts.wzy.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 我的自营商品表 前端控制器
 * </p>
 *
 * @author zhiyong
 * @since 2021-03-19
 */
@Api(tags = "我的自营商品表 前端控制器",position = 20)
@ApiSort(value = 10)
@RestController
@RequestMapping("/SelfModule/self-my-goods")
public class SelfMyGoodsController {
    @Autowired
    private ISelfMyGoodsService selfMyGoodsService;
    @Autowired
    private ISelfGoodsService selfGoodsService;

    @ApiOperation(value = "添加商品到我的自营商品库")
    @RequestMapping(value = "lmToMyGoods", method = RequestMethod.POST)
    public Result<SelfMyGoods> lmToMyGoods(@RequestBody LmToMyGoodsRequest request) {

        MyLambdaQueryWrapper<SelfGoods> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.eq(SelfGoods::getItemId, request.getItemId());
        wrapper.eq(BaseEntity::getDelFlag, "0");

        SelfGoods lmGoods = selfGoodsService.getOne(wrapper);

        if (lmGoods == null) {
            return Result.buildResult(500, "商品不存在");
        }

        SelfMyGoods lmSelfGoods = new SelfMyGoods();

        CopyUtils.copyProperties(request, lmSelfGoods);
        CopyUtils.copyProperties(lmGoods, lmSelfGoods);
        selfMyGoodsService.save(lmSelfGoods);
        return Result.buildResult(lmSelfGoods);
    }

    @ApiOperation(value = "id删除")
    @RequestMapping(value = "delById", method = RequestMethod.POST)
    public Result<SelfMyGoods> delById(@RequestBody BaseIdParam t) {
        SelfMyGoods t1 = selfMyGoodsService.getById(t.getId());
        if (t1 != null) {
            t1.setDelFlag("1");
            selfMyGoodsService.updateById(t1);
            return Result.buildResult();
        }
        return Result.buildResult(500, "id = " + t.getId() + " 不存在");

    }


    @ApiOperation(value = "分页获得我的自营商品库")
    @RequestMapping(value = "getPage", method = RequestMethod.POST)
    public Result<MyPage<SelfMyGoods>> getPage(@RequestBody PageSelfMyGoodsRequest request) {


        SelfMyGoods selfGoods=new SelfMyGoods();
        CopyUtils.copyProperties(request,selfGoods);

        selfGoods.setItemId(null);

        MyLambdaQueryWrapper<SelfMyGoods> wrapper = new MyLambdaQueryWrapper<>(selfGoods);

        if(StringUtils.isNotEmpty(request.getItemTitle())){
            wrapper.like(SelfMyGoods::getItemTitle,request.getItemTitle());
        }

        wrapper.eq(BaseEntity::getDelFlag, "0");

        wrapper.orderByDesc(BaseEntity::getCreateDate);

        MyPage<SelfMyGoods> lmSelfGoodsList = selfMyGoodsService.page(wrapper, request.getCurrentPage(), request.getPageSize(), true);

        return Result.buildResult(lmSelfGoodsList);
    }

}
